<html>
<head><meta charset="utf-8"><title>Reviving slice::take · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Reviving.20slice.3A.3Atake.html">Reviving slice::take</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="210793212"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Reviving%20slice%3A%3Atake/near/210793212" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Vermeulen <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Reviving.20slice.3A.3Atake.html#210793212">(Sep 21 2020 at 20:11)</a>:</h4>
<p>The <code>slice::take</code> PR (<a href="https://github.com/rust-lang/rust/pull/62282">https://github.com/rust-lang/rust/pull/62282</a>) was closed due to inactivity earlier this year when it was very close to being merged. I'd love to push this forward, what would that entail? Do I just create a new PR with the same changes?</p>



<a name="210796133"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Reviving%20slice%3A%3Atake/near/210796133" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Reviving.20slice.3A.3Atake.html#210796133">(Sep 21 2020 at 20:33)</a>:</h4>
<p>(I'm amused that the first line of that PR is "A previous PR that would have added some of this functionality was closed due to inactivity: <a href="https://github.com/rust-lang/rust/issues/49173">#49173</a>")</p>



<a name="210796289"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Reviving%20slice%3A%3Atake/near/210796289" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Reviving.20slice.3A.3Atake.html#210796289">(Sep 21 2020 at 20:35)</a>:</h4>
<p>question: what's the use case compared to a slice pattern?</p>



<a name="210797224"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Reviving%20slice%3A%3Atake/near/210797224" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Reviving.20slice.3A.3Atake.html#210797224">(Sep 21 2020 at 20:43)</a>:</h4>
<p>But yes, new unstable methods probably just needs a new PR addressing whatever kept the previous one from going in.</p>



<a name="210797531"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Reviving%20slice%3A%3Atake/near/210797531" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Reviving.20slice.3A.3Atake.html#210797531">(Sep 21 2020 at 20:45)</a>:</h4>
<p>(And maybe some code modernization like <code>slice::check_range</code>; unsure.)</p>
<p>Shame that needs a new <code>OneSidedRange</code> trait, that that it's for a bad reason...</p>



<a name="210798401"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Reviving%20slice%3A%3Atake/near/210798401" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Vermeulen <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Reviving.20slice.3A.3Atake.html#210798401">(Sep 21 2020 at 20:53)</a>:</h4>
<p>Why is that a shame, seems like a pretty elegant solution to me?</p>



<a name="210799407"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Reviving%20slice%3A%3Atake/near/210799407" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Vermeulen <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Reviving.20slice.3A.3Atake.html#210799407">(Sep 21 2020 at 21:01)</a>:</h4>
<p><span class="user-mention" data-user-id="281757">@Jubilee</span> <code>slice::take</code> is mostly an ergonomics improvement over <code>slice::split_at</code> where you reassign one of the parts back to the slice, especially with mutable slices this can be a bit annoying. The <code>slice::Chunks</code> iterator types could especially benefit from this, code like</p>
<div class="codehilite"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">tmp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">mem</span>::<span class="n">replace</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">v</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="p">[]);</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="p">(</span><span class="n">head</span><span class="p">,</span><span class="w"> </span><span class="n">tail</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tmp</span><span class="p">.</span><span class="n">split_at_mut</span><span class="p">(</span><span class="n">sz</span><span class="p">);</span><span class="w"></span>
<span class="bp">self</span><span class="p">.</span><span class="n">v</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tail</span><span class="p">;</span><span class="w"></span>
<span class="nb">Some</span><span class="p">(</span><span class="n">head</span><span class="p">)</span><span class="w"></span>
</code></pre></div>


<p>is a one-liner with <code>slice::take</code>.</p>



<a name="210800421"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Reviving%20slice%3A%3Atake/near/210800421" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Reviving.20slice.3A.3Atake.html#210800421">(Sep 21 2020 at 21:09)</a>:</h4>
<p>mmm, It is a lot of methods on slice which then go to Vec... I already struggle to read the docs for it.</p>



<a name="210800718"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Reviving%20slice%3A%3Atake/near/210800718" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Vermeulen <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Reviving.20slice.3A.3Atake.html#210800718">(Sep 21 2020 at 21:11)</a>:</h4>
<p><code>take</code> is a method on <code>&amp;mut &amp;[T]</code>, not on <code>&amp;[T]</code>, so it shouldn't go to <code>Vec</code>.</p>



<a name="210801332"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Reviving%20slice%3A%3Atake/near/210801332" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Reviving.20slice.3A.3Atake.html#210801332">(Sep 21 2020 at 21:17)</a>:</h4>
<p>methods on [u8] appear in the documentation for Vec&lt;T&gt;, I wouldn't expect a difference here.</p>



<a name="210802513"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Reviving%20slice%3A%3Atake/near/210802513" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Vermeulen <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Reviving.20slice.3A.3Atake.html#210802513">(Sep 21 2020 at 21:28)</a>:</h4>
<p>Those methods are actually callable on (certain) vectors, these are not</p>



<a name="210802929"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Reviving%20slice%3A%3Atake/near/210802929" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Reviving.20slice.3A.3Atake.html#210802929">(Sep 21 2020 at 21:32)</a>:</h4>
<p><del>8</del> 7 methods is still a lot of API real estate on std::slice.</p>



<a name="210834039"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Reviving%20slice%3A%3Atake/near/210834039" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Reviving.20slice.3A.3Atake.html#210834039">(Sep 22 2020 at 07:04)</a>:</h4>
<p><span class="user-mention" data-user-id="220645">@Tim Vermeulen</span> oops, *"not that it's for a bad reason".</p>
<p>It's just one of those things that I wouldn't hesitate to do in my own code but that seems to always make an addition more complicated for <code>core</code>.  It leads to questions like "should it be public? what if people want to wrap it?" etc -- see how <code>SliceIndex</code> is a stable trait with only (probably-perma-)unstable methods, for example.</p>



<a name="210834331"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Reviving%20slice%3A%3Atake/near/210834331" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Reviving.20slice.3A.3Atake.html#210834331">(Sep 22 2020 at 07:08)</a>:</h4>
<p>Also, now that <code>slice::Iter&lt;'a, T&gt;</code> is both <code>AsRef&lt;[T]&gt;</code> and has <code>.as_slice()</code>, it makes me think that anything using <code>.take_first()</code> on a <code>&amp;mut &amp;'a [T]</code> should probably be calling <code>.next()</code> on a <code>&amp;mut Iter&lt;'a, T&gt;</code> instead...</p>



<a name="210835099"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Reviving%20slice%3A%3Atake/near/210835099" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Vermeulen <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Reviving.20slice.3A.3Atake.html#210835099">(Sep 22 2020 at 07:20)</a>:</h4>
<p><code>slice::take</code> could technically also take a normal range and first do a rotate to make whatever's outside of that range contiguous. That does turn it into quite a different operation though (with different performance) so I think a new trait is the right call, maybe we'll even find uses for it elsewhere.</p>



<a name="210838102"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Reviving%20slice%3A%3Atake/near/210838102" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Vermeulen <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Reviving.20slice.3A.3Atake.html#210838102">(Sep 22 2020 at 08:00)</a>:</h4>
<p>I'm quite indifferent about <code>take_first</code>/<code>take_last</code>, but I don't think you can always just use <code>Iter</code> either, sometimes for other purposes it makes sense to really store it as a slice (e.g. see <code>slice::Split</code> which could benefit from <code>take_first</code> inside <code>next</code>). Converting back and forth is often not worth the trouble.</p>



<a name="210856507"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Reviving%20slice%3A%3Atake/near/210856507" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Vermeulen <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Reviving.20slice.3A.3Atake.html#210856507">(Sep 22 2020 at 11:24)</a>:</h4>
<p>Some tests in that PR don't compile in a reasonable amount of time because they contain <code>&amp;[(); usize::MAX]</code>, is that not supposed to be fine?</p>



<a name="210861306"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Reviving%20slice%3A%3Atake/near/210861306" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Vermeulen <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Reviving.20slice.3A.3Atake.html#210861306">(Sep 22 2020 at 12:19)</a>:</h4>
<p>Or does the slice length limit of <code>isize::MAX</code> also apply to ZST?</p>



<a name="210861338"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Reviving%20slice%3A%3Atake/near/210861338" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Vermeulen <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Reviving.20slice.3A.3Atake.html#210861338">(Sep 22 2020 at 12:19)</a>:</h4>
<p>Or maybe that's only when they're from a <code>Vec</code>...</p>



<a name="210861964"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Reviving%20slice%3A%3Atake/near/210861964" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Reviving.20slice.3A.3Atake.html#210861964">(Sep 22 2020 at 12:25)</a>:</h4>
<p>The limit of <code>isize::MAX</code> concerns the allocation size, i.e. to <code>size_of_val</code>, not the slice length.<br>
Even a vector of isize::MAX ZSTs is fine.</p>



<a name="210863536"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Reviving%20slice%3A%3Atake/near/210863536" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Vermeulen <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Reviving.20slice.3A.3Atake.html#210863536">(Sep 22 2020 at 12:38)</a>:</h4>
<p>Thanks for clarifying, in that case I'm not sure why <code>&amp;[(); usize::MAX]</code> doesn't compile in a reasonable amount of time. The playground also times out on it (<a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=d5acc3bab9d1002851321d90693cb806">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=d5acc3bab9d1002851321d90693cb806</a>)</p>



<a name="210891278"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Reviving%20slice%3A%3Atake/near/210891278" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Quy Nguyen <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Reviving.20slice.3A.3Atake.html#210891278">(Sep 22 2020 at 15:56)</a>:</h4>
<p>Looks like a compiler bug to me. Actually I'm not sure hmm.</p>



<a name="210891945"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Reviving%20slice%3A%3Atake/near/210891945" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Reviving.20slice.3A.3Atake.html#210891945">(Sep 22 2020 at 16:00)</a>:</h4>
<p>I'd report a bug, yeah</p>



<a name="210892682"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Reviving%20slice%3A%3Atake/near/210892682" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Quy Nguyen <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Reviving.20slice.3A.3Atake.html#210892682">(Sep 22 2020 at 16:05)</a>:</h4>
<p>From godbolt earlier versions of Rust died with OOM and newer versions take a very long time. Looks like a very old bug.</p>



<a name="210894742"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Reviving%20slice%3A%3Atake/near/210894742" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> DPC <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Reviving.20slice.3A.3Atake.html#210894742">(Sep 22 2020 at 16:19)</a>:</h4>
<p>please report it nevertheless</p>



<a name="210895137"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Reviving%20slice%3A%3Atake/near/210895137" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Quy Nguyen <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Reviving.20slice.3A.3Atake.html#210895137">(Sep 22 2020 at 16:22)</a>:</h4>
<p>Already doing :P</p>



<a name="210898705"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Reviving%20slice%3A%3Atake/near/210898705" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Vermeulen <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Reviving.20slice.3A.3Atake.html#210898705">(Sep 22 2020 at 16:48)</a>:</h4>
<p>Thanks!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>